package 力扣91;

/**
 * @ClassName : Day8_24_两两交换链表中的节点
 * @Author : 骆发茂
 * @Date: 2021/12/19 20:06
 * @Description : https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 *
 * https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/yuan-lai-hui-luo-ji-qing-xi-jian-dan-yi-8t93h/
 */
public class Day8_24_两两交换链表中的节点 {
    class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode resultHead = new ListNode();//虚拟头节点
            resultHead.next = head;
            ListNode curNode = resultHead;

            while(curNode != null && curNode.next != null && curNode.next.next != null) {
                ListNode first = curNode;//刚开始指向虚拟头节点
                ListNode  second= first.next;//刚开始指向第一个节点
                ListNode third = second.next;//刚开始指向第二个节点

                first.next = third;
                second.next = third.next;
                third.next = second;

                curNode = curNode.next.next;
        }
            return resultHead.next;
    }
}
}